package com.vito.oa.act.service.impl;

import static com.vito.oa.act.constant.OaActContants.CACHE_ACT_VALUE;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;

import com.alibaba.dubbo.config.annotation.Service;
import com.vito.base.model.DataGrid;
import com.vito.base.model.MyHashMap;
import com.vito.oa.act.dao.OaActCCMapper;
import com.vito.oa.act.extend.model.OaActExApproveInfoModel;
import com.vito.oa.act.service.OaActCCService;

@Service
public class OaActCCServiceImpl implements OaActCCService {
	@Autowired
	private OaActCCMapper oaActCCMapper;

	@Override
	@Cacheable(value = CACHE_ACT_VALUE, key = "#root.method.name+'_'+#businessId")
	public List<Map<String, String>> queryCCUsers(String businessId) {
		return oaActCCMapper.queryCCUsers(businessId);
	}

	@Override
	@Cacheable(value = CACHE_ACT_VALUE, key = OaActExApproveInfoModel.CACHE_KEY)
	public DataGrid<MyHashMap<String, String>> queryCCtoMePage(OaActExApproveInfoModel actExApproveInfoModel) {
		DataGrid<MyHashMap<String, String>> data = new DataGrid<MyHashMap<String, String>>();
		data.setRows(oaActCCMapper.queryCCtoMeByPage(actExApproveInfoModel));
		data.setTotal(actExApproveInfoModel.getTotal());
		return data;
	}

	@Override
	public boolean saveProcessCopyPerson(Map<String, Object> map) {
		@SuppressWarnings("unchecked")
		List<String> list = (List<String>) map.get("userIds");
		if (list.size() == 0) {
			oaActCCMapper.delProcessCopyPerson(map);
			return true;
		} else {
			List<String> userList = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++) {
				if (!userList.contains(list.get(i))) {
					userList.add(list.get(i));
				}
			}
			map.put("userIds", userList);
			oaActCCMapper.delProcessCopyPerson(map);
			return oaActCCMapper.saveProcessCopyPerson(map) > 0 ? true : false;
		}

	}

	@Override
	public List<String> queryProcessCopyPerson(Map<String, String> map) {
		return oaActCCMapper.queryProcessCopyPerson(map);
	}

}
